package matchthreegame.handlers;

import java.lang.ref.WeakReference;

import matchthreegame.dataBase.DBManagerHelper;
import matchthreegame.extensions.MatchThreeGameZoneExtension;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

public class UpgradeHeroRequestHandler extends BaseClientRequestHandler {
	
	public static final String UPGRADE_TYPE = "UT";
	public static final String HERO_TYPE    = "HT";
	
	MatchThreeGameZoneExtension zoneExtension;

	@Override
	public void handleClientRequest(User sender, ISFSObject params) {
		
		zoneExtension = (MatchThreeGameZoneExtension)getParentExtension().getParentZone().getExtension();
		WeakReference<DBManagerHelper> dbHelper = null;
		
		String senderName = sender.getName();
		int heroType = params.getInt(HERO_TYPE);
		int upgradeType = params.getInt(UPGRADE_TYPE);
				
		try {
			dbHelper = new WeakReference<DBManagerHelper>(zoneExtension.getDBHelper());
			
			// Upgrade hero for sender
			dbHelper.get().upgradeHero(senderName, heroType, upgradeType);
			
		} catch (Exception e) {
			trace(e.getMessage().toString());
		}
		
	}

}
